package com.hpp.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hpp.model.SysRole;
import com.hpp.service.IRoleService;
import com.hpp.utils.DataTableModel;
import com.hpp.utils.DateUtils;
import com.hpp.utils.RespJson;
import com.hpp.utils.ResultCode;

@Controller
@RequestMapping(value = "/role")
public class RoleController {
	
	private Logger logger = LoggerFactory.getLogger(RoleController.class);
	
	@Autowired
	IRoleService roleService;
	
	/**
	 * @Title getRoleList  
	 * @Description 获取角色列表
	 * @author hanpp
	 * @param draw
	 * @param start
	 * @param length
	 * @return DataTableModel<?>
	 * @date 2019年1月10日 上午10:29:40  
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/page/list",method = RequestMethod.POST)
	public DataTableModel<?> getRoleList(
			@RequestParam(value = "name",required = false) String name,
			int draw,int start,int length){
		DataTableModel<SysRole> result = new DataTableModel<SysRole>();
		result.setDraw(draw);
		PageHelper.startPage(start / length + 1, length);
		HashMap<String, Object> map = new HashMap<String,Object>();
		if (StringUtils.isNotBlank(name)) {
			map.put("name", name);
		}
		List<SysRole> list = roleService.getSysRoleList(map);
		PageInfo<SysRole> pageList = new PageInfo<SysRole>(list);
		if (pageList != null && pageList.getEndRow() != 0) {
			result.setData(pageList.getList());
			result.setRecordsFiltered(new Long(pageList.getTotal()).intValue());
			result.setRecordsTotal(new Long(pageList.getTotal()).intValue());
		}
		return result;
	}
	
	/**
	 * @Title getRoleList  
	 * @Description 获取角色列表
	 * @author hanpp
	 * @return RespJson
	 * @date 2019年1月11日 下午4:57:05  
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/list",method = RequestMethod.POST)
	public RespJson getRoleList(){
		HashMap<String, Object> map = new HashMap<String,Object>();
		List<SysRole> list = roleService.getSysRoleList(map);
		return new RespJson(true, "操作成功", ResultCode.SUCCESS, list);
	}
	
	/**
	 * @Title operateROle  
	 * @Description 
	 * @author hanpp
	 * @param id
	 * @param name
	 * @param remark
	 * @return RespJson
	 * @date 2019年1月10日 上午11:56:33  
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/operate",method = RequestMethod.POST)
	public RespJson operateROle(
			@RequestParam(value = "id",required = false) Integer id,
			@RequestParam(value = "roleName",required = true) String roleName,
			@RequestParam(value = "remark",required = false) String remark){
		try {
			HashMap<String, Object> map = new HashMap<String,Object>();
			map.put("name", roleName);
			SysRole role = roleService.getSysRole(map);
			if (role != null) {
				return new RespJson(false, "用户角色已存在", ResultCode.ERROR, "");
			} else {
				role = new SysRole();
				role.setName(roleName);
				if (StringUtils.isNotBlank(remark)) {
					role.setRemark(remark);
				}
				role.setCreateTime(DateUtils.formatDate(new Date(), DateUtils.simple));
				if (null == id) {
					roleService.addSysRole(role);
					return new RespJson(true, "新增成功", ResultCode.SUCCESS, "");
				} else {
					role.setId(id);
					roleService.updateSysRole(role);
					return new RespJson(true, "修改成功", ResultCode.SUCCESS, "");
				}
			}
		} catch (Exception e) {
			logger.error("操作角色失败：" + e);
			return new RespJson(false, "操作失败", ResultCode.ERROR, "");
		}
	}
	
	/**\
	 * @Title getRoleInfo  
	 * @Description 获取角色详情
	 * @author hanpp
	 * @param id
	 * @return RespJson
	 * @date 2019年1月11日 上午9:42:17  
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/info",method = RequestMethod.POST)
	public RespJson getRoleInfo(
			@RequestParam(value = "id",required = true) Integer id){
		try {
			HashMap<String, Object> map = new HashMap<String,Object>();
			map.put("id", id);
			SysRole role = roleService.getSysRole(map);
			return new RespJson(true, "获取成功", ResultCode.SUCCESS, role);
		} catch (Exception e) {
			logger.error("获取角色信息失败：" + e);
			return new RespJson(false, "获取失败", ResultCode.ERROR, "");
		}
	}
	
	/**
	 * @Title deleteRole  
	 * @Description 批量删除角色
	 * @author hanpp
	 * @param ids
	 * @return RespJson
	 * @date 2019年1月11日 上午10:24:11  
	 * @throws
	 */
	@ResponseBody
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	public RespJson deleteRole(
			@RequestParam(value = "ids",required = true) String ids){
		try {
			HashMap<String, Object> map = new HashMap<String,Object>();
			map.put("list", ids.split(","));
			roleService.deleteSysRole(map);
			return new RespJson(true, "删除成功", ResultCode.SUCCESS, "");
		} catch (Exception e) {
			logger.error("删除角色信息失败：" + e);
			return new RespJson(false, "删除失败", ResultCode.ERROR, "");
		}
	}
}
